<template>
  <div class="login" :style="bacpic">
    <div class="box">
      <div class="title">用户登录</div>
      <el-form
        class="register-form"
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
      >
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入账号"
            v-model="ruleForm.phone"
            size="large"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            placeholder="请输入密码"
            size="large"
            v-model="ruleForm.pass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <div class="yanzheng">
          <div class="ipt">
            <input type="text" v-model="yanzhengma" placeholder="验证码" />
          </div>
          <div @click="refreshCode">
            <SIdentify :identifyCode="identifyCode" />
          </div>
        </div>
        <div class="text">
          <span @click="goRegister">去注册</span>
          <span>忘记密码？</span>
        </div>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import SIdentify from "../components/SIdentify.vue";
import { reactive, ref, onMounted } from "vue";
import { user_login } from "../api/index";
import bac from "../assets/bac.png";
import { useRouter } from "vue-router";
var $router = useRouter();
var ruleFormRef = ref();
const ruleForm = reactive({ phone: "", pass: "" });

const validatePhone = (rule, value, callback) => {
  if (/^1[3-9]\d{9}$/.test(value) == false) {
    callback(new Error("请输入正确的手机号"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (/^\d{4}$/.test(value) == false) {
    callback(new Error("请输入符合要求的密码,必须是4位数字"));
  } else {
    callback();
  }
};
//表单验证规则
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: "blur" }],
  pass: [{ validator: validatePass, trigger: "blur" }],
});

var bacpic = ref({
  backgroundImage: "url(" + bac + ")",
  backgroundRepeat: "no-repeat",
  backgroundSize: "cover",
  minHeight: "100vh",
  position: "relative",
  backgroundPosition: "center center",
});
var goRegister = () => {
  $router.push("/register");
};

//保存验证码
var yanzhengma = ref("");
// 图形验证码
let identifyCodes = "1234567890";
let identifyCode = ref("3212");
const randomNum = (min, max) => {
  return Math.floor(Math.random() * (max - min) + min);
};

const makeCode = (o, l) => {
  for (let i = 0; i < l; i++) {
    identifyCode.value += o[randomNum(0, o.length)];
  }
};

const refreshCode = () => {
  identifyCode.value = "";
  makeCode(identifyCodes, 4);
};

onMounted(() => {
  identifyCode.value = "";
  makeCode(identifyCodes, 4);
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid && yanzhengma.value == identifyCode.value) {
      user_login(ruleForm).then((res) => {
        if (res.data.code == 200) {
          ElNotification({
            message: res.data.msg,
            type: "success",
          });
          localStorage.setItem("token", res.data.token);
          localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));
          $router.push("/index");
        } else {
          ElNotification({
            message: res.data.msg,
            type: "error",
          });
        }
      });
    } else if (!yanzhengma.value || yanzhengma.value !== identifyCode.value) {
      ElNotification({
        message: "验证失败！",
        type: "error",
      });
    } else {
      return false;
    }
  });
};
</script>

<style scoped lang="scss">
.login {
  overflow: hidden;
  .box {
    width: 300px;
    margin: 200px auto;
    background-color: white;
    padding: 20px 40px 20px;
    .title {
      line-height: 30px;
    }
    .ipt {
      line-height: 40px;
      input {
        width: 100%;
        height: 30px;
        border: 1px solid #ccc;
        outline: none;
        border-radius: 5px;
        padding-left: 5px;
        color: #ccc;
        font-size: 12px;
      }
    }
    .yanzheng {
      display: flex;
      justify-content: space-between;
    }
    .text {
      line-height: 25px;
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      color: lightskyblue;
      cursor: pointer;
    }
    .btn {
      margin: 30px auto;
      padding: 0 98px;
    }
  }
}
</style>